<?php
namespace app\agent\controller;

use app\common\controller\AgentBaseController;
use app\common\enum\IsDelete;
use app\common\ext\IDb;
use app\common\ext\IRequest;
use think\Db;

class CreditTotal extends AgentBaseController{

    /**
     * @功能：根据用户统计收藏
     * @开发者：cxl
     * @return string
     */
    public function user_favorite_total(){
        //接收参数
        $user_id = IRequest::get("user_id");
        $nick = IRequest::get("nick");
        $phone = IRequest::get("phone");

        //判断是否为用户搜索
        $credit_favorite_where['is_delete'] = IsDelete::No;
        if(!empty($credit_product_id)){
            $credit_favorite_where['user_id'] = $user_id;
        }else {
            if (empty($user_id) && (!empty($nick) || !empty($phone))) {
                //查询条件
                $user_where = null;
                if (!empty($nick)) $user_where['nick'] = array("like", "%{$nick}%");
                if (!empty($phone)) $user_where['phone'] = array("like", "%{$phone}%");

                //取得用户信息
                $user_list = IDb::getInstance("user")->setDbWhere($user_where)->sel();
                if (!empty($user_list) && is_array($user_list)) {
                    $credit_favorite_where['user_id'] = array("in", array_column($user_list, "user_id"));
                }
            }
        }

        //取得数据
        $credit_favorite_list = IDb::getInstance("credit_favorite")
            ->setDbFiled("user_id,count(*) as favorite_num")
            ->setDbWhere($credit_favorite_where)
            ->setDbGroup("user_id")
            ->setDbOrder("favorite_num desc,user_id")
            ->pag();

        //页面赋值
        $this->assign("user_id",$user_id);
        $this->assign("nick",$nick);
        $this->assign("phone",$phone);
        $this->assign("credit_favorite_list",$credit_favorite_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：信用卡收藏记录
     * @开发者：cxl
     * @return string
     */
    public function user_favorite_list(){
        //接收参数
        $user_id = IRequest::get("user_id",IRequest::NOT_EMPTY,"用户编号不能为空！");

        //取得数据
        $credit_favorite_where['is_delete'] = IsDelete::No;
        $credit_favorite_where['user_id'] = $user_id;
        $credit_favorite_list = IDb::getInstance("credit_favorite")
            ->setDbWhere($credit_favorite_where)
            ->setDbOrder("credit_favorite_id")
            ->pag();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("user_id",$user_id);
        $this->assign("credit_favorite_list",$credit_favorite_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：根据信用卡统计收藏
     * @开发者：cxl
     * @return string
     */
    public function credit_favorite_total(){
        //接收参数
        $credit_product_id = IRequest::get("credit_product_id");
        $credit_product_name = IRequest::get("credit_product_name");

        //判断是否为用户搜索
        $credit_favorite_where['cf.is_delete'] = IsDelete::No;
        $credit_favorite_where['cp.agent_id'] = get_login_agent_id();
        if (!empty($credit_product_id)) $credit_favorite_where['cf.credit_product_id'] = $credit_product_id;
        if (!empty($credit_product_name)) $credit_favorite_where['cp.credit_product_name'] = array("like", "%{$credit_product_name}%");

        //取得数据
        $credit_favorite_list = IDb::getInstance("credit_favorite as cf")
            ->setDbFiled("cf.credit_product_id,count(*) as favorite_num")
            ->setDbJoin('credit_product as cp','cp.credit_product_id = cf.credit_product_id')
            ->setDbWhere($credit_favorite_where)
            ->setDbGroup("cf.credit_product_id")
            ->setDbOrder("favorite_num desc,cf.credit_product_id")
            ->pag();
        if ($credit_favorite_list === false){
            error('获取收藏统计失败');
        }

        //页面赋值
        $this->assign("credit_product_id",$credit_product_id);
        $this->assign("credit_product_name",$credit_product_name);
        $this->assign("credit_favorite_list",$credit_favorite_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：根据信用卡收藏记录
     * @开发者：cxl
     * @return string
     */
    public function credit_favorite_list(){
        //接收参数
        $credit_product_id = IRequest::get("credit_product_id",IRequest::NOT_EMPTY,"用户编号不能为空！");

        //取得数据
        $credit_favorite_where['is_delete'] = IsDelete::No;
        $credit_favorite_where['credit_product_id'] = $credit_product_id;
        $credit_favorite_list = IDb::getInstance("credit_favorite")
            ->setDbWhere($credit_favorite_where)
            ->setDbOrder("credit_favorite_id")
            ->pag();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("credit_product_id",$credit_product_id);
        $this->assign("credit_favorite_list",$credit_favorite_list);

        //渲染模板输出
        return view();
    }
}
